package com.ece.pfe_rover_pvt_ines_ece_application.view;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.ece.pfe_rover_pvt_ines_ece_application.PFE_ROVER_PVT_INES_ECE_Application;
import com.ece.pfe_rover_pvt_ines_ece_application.R;
import com.ece.pfe_rover_pvt_ines_ece_application.model.Measurements;
import com.parse.GetCallback;
import com.parse.ParseException;
import com.parse.ParseQuery;
import com.parse.ParseUser;
import com.parse.SaveCallback;

/**
 * 
 * @author mathieubakrim
 * Class associated to the creation of a new measurement.
 * Should be deleted once loading from SD card or Bluetooth is available.
 */
public class NewMeasurementsActivity extends Activity {

	private static final boolean SHOW_BUTTON = true;
	private Button saveButton;
	private Button deleteButton;
	private EditText measurementsText0,
					 measurementsText1,
					 measurementsText2,
					 measurementsText3,
					 measurementsText4,
					 measurementsText5,
					 measurementsText6,
					 measurementsText7,
					 measurementsText8,
					 measurementsText9,
					 measurementsText10;
	@SuppressWarnings("unused")
	private List<EditText> measurementTextList;
	private Measurements measurements;
	private String measurementsId = null;
	
	public NewMeasurementsActivity() {
		super();
		this.measurementTextList = new ArrayList<EditText>();
	}
			
	/**
	 * OnCreate (Android method related to Activity class)
	 * Compose the view (layouts, buttons...)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_measurements);

		ActionBar actionBar = getActionBar();
		actionBar.setDisplayHomeAsUpEnabled(true);		
		
		// Fetch the measurementsId from the Extra data
		if (getIntent().hasExtra("ID")) {
			measurementsId = getIntent().getExtras().getString("ID");
		}
		measurementsText0 = (EditText) findViewById(R.id.measurement_0);
		measurementsText1 = (EditText) findViewById(R.id.measurement_1);
		measurementsText2 = (EditText) findViewById(R.id.measurement_2);
		measurementsText3 = (EditText) findViewById(R.id.measurement_3);
		measurementsText4 = (EditText) findViewById(R.id.measurement_4);
		measurementsText5 = (EditText) findViewById(R.id.measurement_5);
		measurementsText6 = (EditText) findViewById(R.id.measurement_6);
		measurementsText7 = (EditText) findViewById(R.id.measurement_7);
		measurementsText8 = (EditText) findViewById(R.id.measurement_8);
		measurementsText9 = (EditText) findViewById(R.id.measurement_9);
		measurementsText10 = (EditText) findViewById(R.id.measurement_10);	
		
		saveButton = (Button) findViewById(R.id.saveButton);
		deleteButton = (Button) findViewById(R.id.deleteButton);

		
		
		if (measurementsId == null) {
			measurements = new Measurements();
			measurements.setUuidString();
		} else {
			ParseQuery<Measurements> query = Measurements.getQuery();
			query.fromLocalDatastore();
			query.whereEqualTo("uuid", measurementsId);
			query.getFirstInBackground(new GetCallback<Measurements>() {

				
				
				@Override
				public void done(Measurements object, ParseException e) {
					if (!isFinishing()) {
						measurements = object;
						GregorianCalendar date = new GregorianCalendar();
						SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm");
												
						date.setTimeInMillis(measurements.getDate());
						
						measurementsText0.setText(Integer.toString(measurements.getEntryId()));
						
						measurementsText1.setText(sdf.format(date.getTime()));
						
						measurementsText2.setText(Double.toString(measurements.getI()));
						measurementsText3.setText(Double.toString(measurements.getV()));
						measurementsText4.setText(Double.toString(measurements.getLum()));						
						measurementsText5.setText(Double.toString(measurements.getTair()));						
						measurementsText6.setText(Double.toString(measurements.getSwind()));
						measurementsText7.setText(Double.toString(measurements.getP()));
						measurementsText8.setText(measurements.getGPS().toString());
						measurementsText9.setText(Integer.toString(measurements.getInputId())); // IdPan
						measurementsText10.setText(Double.toString(measurements.getTpan()));
																								
						if(SHOW_BUTTON){
							deleteButton.setVisibility(View.VISIBLE);
						} else if (ParseUser.getCurrentUser().getObjectId().equals(PFE_ROVER_PVT_INES_ECE_Application.ADMIN_ID)) {
							deleteButton.setVisibility(View.VISIBLE);
						}
					}
				}
			});
		}
		
		// Save a measurements list with its parameters (into measurements object
		saveButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				measurements.setEntryId(Integer.parseInt(measurementsText0.getText().toString()));
				measurements.setI(Double.parseDouble(measurementsText2.getText().toString()));
				measurements.setV(Double.parseDouble(measurementsText3.getText().toString()));
				measurements.setLum(Double.parseDouble(measurementsText4.getText().toString()));
				measurements.setTair(Double.parseDouble(measurementsText5.getText().toString()));
				measurements.setSwind(Double.parseDouble(measurementsText6.getText().toString()));
				measurements.setP(Double.parseDouble(measurementsText7.getText().toString()));
				//measurements.setDate(Long.parseLong(measurementsText1.getText().toString()));
				measurements.setGPS("N/A");
				measurements.setInputId(Integer.parseInt(measurementsText9.getText().toString())); // IdPan
				measurements.setTpan(Double.parseDouble(measurementsText10.getText().toString()));				
				
				measurements.setDraft(true);
				measurements.setAuthor(ParseUser.getCurrentUser());
				measurements.pinInBackground(PFE_ROVER_PVT_INES_ECE_Application.MEASUREMENTS_GROUP_NAME,
						new SaveCallback() {
							@Override
							public void done(ParseException e) {
								if (isFinishing()) {
									return;
								}
								if (e == null) {
									setResult(Activity.RESULT_OK);
									finish();
								} else {
									Toast.makeText(getApplicationContext(),
											"Error saving: " + e.getMessage(),
											Toast.LENGTH_LONG).show();
								}
							}
						});
			}
		});

		deleteButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// The measurements will be deleted eventually but will
				// immediately be excluded from query results.
				measurements.deleteEventually();
				setResult(Activity.RESULT_OK);
				finish();
			}
		});

		if(!SHOW_BUTTON){
			saveButton.setVisibility(View.INVISIBLE);
			deleteButton.setVisibility(View.INVISIBLE);
		}	
	}
}
